package com.example.neochevbook;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AppartementAdapter extends BaseAdapter {

	private List<Appartement> Listec;
	private LayoutInflater inflater;
	
	public AppartementAdapter(Afficheappartement afficheappartement, Modele malistec)
	{
		
	}
	
	public AppartementAdapter(Context context,List<Appartement> vListec) {
		inflater = LayoutInflater.from(context);
		Listec = vListec;
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return Listec.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return Listec.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
	
	private class ViewHolder {
		TextView vid_bien, vtitre, vdescription, vcharges, vprix, vsuperficie, 
		vadresse, vville, vcode_postal, vdepartement, vpieces, vchambres, vtoilettes, vsalle_de_bains, 
		vetages, vascenseur, vdate_creation, vdate_modification, vlatitude, vlongitude, 
		vdate_disponibilite, vid_utilisateur;
		ImageView vimageappartement;
	}

	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;

		if(convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.vueappartement, null);
			holder.vimageappartement = (ImageView)convertView.findViewById(R.id.imageappartement);
			holder.vid_bien = (TextView)convertView.findViewById(R.id.id_bien);
			holder.vtitre = (TextView)convertView.findViewById(R.id.titre);
			holder.vdescription = (TextView)convertView.findViewById(R.id.description);
			holder.vcharges = (TextView)convertView.findViewById(R.id.charges);
			holder.vprix = (TextView)convertView.findViewById(R.id.prix);
			holder.vsuperficie = (TextView)convertView.findViewById(R.id.superficie);
			holder.vadresse = (TextView)convertView.findViewById(R.id.adresse);
			holder.vville = (TextView)convertView.findViewById(R.id.Ville);
			holder.vcode_postal = (TextView)convertView.findViewById(R.id.code_postal);
			holder.vdepartement = (TextView)convertView.findViewById(R.id.departement);
			holder.vpieces = (TextView)convertView.findViewById(R.id.piece);
			holder.vchambres = (TextView)convertView.findViewById(R.id.chambres);
			holder.vtoilettes = (TextView)convertView.findViewById(R.id.toilette);
			holder.vsalle_de_bains = (TextView)convertView.findViewById(R.id.salle_de_bain);
			holder.vetages = (TextView)convertView.findViewById(R.id.etage);
			holder.vascenseur = (TextView)convertView.findViewById(R.id.ascenseur);
			holder.vdate_creation = (TextView)convertView.findViewById(R.id.date_creation);
			holder.vdate_modification = (TextView)convertView.findViewById(R.id.date_modification);
			holder.vlatitude = (TextView)convertView.findViewById(R.id.latitude);
			holder.vlongitude = (TextView)convertView.findViewById(R.id.longitude);
			holder.vdate_disponibilite = (TextView)convertView.findViewById(R.id.date_disponibilite);
			holder.vid_utilisateur = (TextView)convertView.findViewById(R.id.id_utilisateur);
			

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
	
		holder.vimageappartement.setImageDrawable(ImageOperations(Listec.get(position).getImage()));
		holder.vtitre.setText(Listec.get(position).getTitre());
		holder.vdescription.setText(Listec.get(position).getDescription());
		holder.vcharges.setText(Double.toString(Listec.get(position).getCharges()));
		holder.vprix.setText(Double.toString(Listec.get(position).getPrix()));
		holder.vsuperficie.setText(Double.toString(Listec.get(position).getSuperficie()));
		holder.vadresse.setText(Listec.get(position).getAdresse());
		holder.vville.setText(Listec.get(position).getVille());
		holder.vcode_postal.setText(Integer.toString(Listec.get(position).getCode_postal()));
		holder.vdepartement.setText(Listec.get(position).getDepartement());
		holder.vpieces.setText(Integer.toString(Listec.get(position).getPieces()));
		holder.vchambres.setText(Integer.toString(Listec.get(position).getChambres()));
		holder.vtoilettes.setText(Integer.toString(Listec.get(position).getToilettes()));
		holder.vsalle_de_bains.setText(Integer.toString(Listec.get(position).getSalle_de_bains()));
		holder.vetages.setText(Integer.toString(Listec.get(position).getEtages()));
		holder.vascenseur.setText(Integer.toString(Listec.get(position).getAscenseur()));
		holder.vdate_creation.setText(Listec.get(position).getDate_creation().toString());
		holder.vdate_modification.setText(Listec.get(position).getDate_modification().toString());
		holder.vlatitude.setText(Double.toString(Listec.get(position).getLatitude()));
		holder.vlongitude.setText(Double.toString(Listec.get(position).getLongitude()));
		holder.vdate_disponibilite.setText(Listec.get(position).getDate_disponibilite().toString());
		holder.vid_utilisateur.setText(Integer.toString(Listec.get(position).getId_utilisateur()));
		

		return convertView;
	}
	
	protected Drawable ImageOperations(String url) {
		try {
			URL urle = new URL(url);
			Object content = urle.getContent();
	 
			InputStream inputstream = (InputStream)content;
			Drawable drawable = Drawable.createFromStream(inputstream, "src");
			return drawable;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}
	 

}
